import scenes from '@NAVIGATION/scenes'
import DBConfig from '@CONFIG/DBConfig'
import Images from '@ASSET/Images'
import { DECISION_TYPE } from '@CONSTANTS/decision'
import { LANGUAGE, SHARE_ROUTE_TYPE } from '@CONSTANTS/app'
import { USER_STATUS } from '@CONSTANTS/profile'
import i18n from '@I18N'

const getShareQuery = ({ myPainManagement, user }) => {
  const { p8Id, Status } = user || {}
  const isLogin = Status === USER_STATUS.Registered
  let query = isLogin ? `invitorUserId=${p8Id}` : ''
  if (myPainManagement?.HasPainManagement) {
    const { Title, TemplateId } = myPainManagement || {}
    query = `${query}&planId=${TemplateId}&planTitle=${encodeURIComponent(Title?.[LANGUAGE.ZH] || '')}`
  }
  return query
}
const buildDoctorDetailMessage = (name, pId, user) => {
  const query = getShareQuery({ user })

  return ({
    title: `${name}医生的个人主页`,
    path: `${scenes.doctorDetail}?pId=${pId}&${query}`
  })
}

const buildDoctorDetailTimeline = (name, pId, user) => {
  const query = getShareQuery({ user })
  return ({
    title: `${name}医生的个人主页`,
    query: `pId=${pId}&${query}`,
    imageUrl: `${DBConfig.staticShareUrl}health_timeline.png`
  })
}

const buildDecisionFlowIndexMessage = (title, id, isSurvey, user) => {
  const query = getShareQuery({ user })
  return ({
    title,
    path: `${scenes.decisionFlowIndex}?title=${title}&dId=${id}&type=${isSurvey ? DECISION_TYPE.Survey : DECISION_TYPE.Decision}&${query}`,
  })
}

const buildDecisionFlowIndexTimeline = (title, id, isSurvey, user) => {
  const query = getShareQuery({ user })
  return ({
    title,
    query: `title=${title}&dId=${id}&type=${isSurvey ? DECISION_TYPE.Survey : DECISION_TYPE.Decision}&${query}`,
    imageUrl: `${DBConfig.staticShareUrl}health_timeline.png`
  })
}

const buildDecisionResultMessage = (title, id, isSurvey, user) => {
  const query = getShareQuery({ user })
  return ({
    title,
    path: `${scenes.decisionResultPage}?id=${id}&isSurvey=${isSurvey}&fromShare=true&${query}`,
  })
}

const buildDecisionResultTimeline = (title, id, isSurvey, user) => {
  const query = getShareQuery({ user })
  return ({
    title,
    query: `title=${title}&id=${id}&isSurvey=${isSurvey}&fromShare=true&${query}`,
    imageUrl: `${DBConfig.staticShareUrl}health_timeline.png`
  })
}

const buildExpertMessage = (title, user) => {
  const query = getShareQuery({ user })
  return ({
    title,
    path: `${scenes.expertPage}?${query}`,
  })
}

const buildExpertTimeline = (title, user) => {
  const query = getShareQuery({ user })
  return ({
    title,
    query,
    imageUrl: `${DBConfig.staticShareUrl}health_timeline.png`
  })
}

const buildHomeMessage = (title, myPainManagement, user) => {
  const query = getShareQuery({ user })
  const path = `${scenes.homePage}?${query}`
  return ({
    title,
    path,
    imageUrl: Images.share.icon_share_home
  })
}

const buildHomeTimeline = (title, myPainManagement, user) => {
  const query = getShareQuery({ user })
  return ({
    title,
    query,
    imageUrl: `${DBConfig.staticShareUrl}health_timeline.png`
  })
}

const buildFeedDetailMessage = (title, id, user) => {
  const query = getShareQuery({ user })
  return ({
    title,
    path: `${scenes.feedDetail}?id=${id}&${query}`,
  })
}

const buildFeedDetailTimeline = (title, id, user) => {
  const query = getShareQuery({ user })
  return ({
    title,
    query: `id=${id}&${query}`,
    imageUrl: `${DBConfig.staticShareUrl}health_timeline.png`
  })
}

const buildVideoDetailMessage = (title, id, user) => {
  const query = getShareQuery({ user })
  return ({
    title,
    path: `${scenes.videoDetail}?id=${id}&${query}`,
  })
}

const buildPlanReportDetailMessage = (title, tId, user) => {
  const query = getShareQuery({ user })
  return ({
    title: `邀请你一起加入${title}!`,
    path: `${scenes.managePlan}?tId=${tId}&${query}`,
    imageUrl: Images.share.share
  })
}

const buildTagDetailMessage = (title, tId, user) => {
  const query = getShareQuery({ user })
  return ({
    title,
    path: `${scenes.tagDetail}?tId=${tId}&${query}`,
  })
}

const buildManagePlanMessage = (title, tId, user) => {
  const query = getShareQuery({ user })
  return ({
    title: `邀请你一起加入${title}!`,
    path: `${scenes.managePlan}?tId=${tId}&${query}`,
    imageUrl: Images.share.share
  })
}

const buildMeditationMessage = (title, user) => {
  const query = getShareQuery({ user })
  return ({
    title,
    path: `${scenes.meditation}?${query}`,
    imageUrl: Images.pain_log.shareMeditation
  })
}

const buildMeditationTimeline = (title, user) => {
  const query = getShareQuery({ user })
  return ({
    title,
    query,
    imageUrl: `${DBConfig.staticShareUrl}health_timeline.png`
  })
}

const buildVideoDetailTimeline = (title, id, user) => {
  const query = getShareQuery({ user })
  return ({
    title,
    query: `id=${id}&${query}`,
    imageUrl: `${DBConfig.staticShareUrl}health_timeline.png`
  })
}

const buildTagDetailTimeline = (title, tId, user) => {
  const query = getShareQuery({ user })
  return ({
    title,
    query: `tId=${tId}&${query}`,
    imageUrl: `${DBConfig.staticShareUrl}health_timeline.png`
  })
}

const buildManagePlanTimeline = (title, tId, user) => {
  const query = getShareQuery({ user })
  return ({
    title: `邀请你一起加入${title}!`,
    query: `tId=${tId}&${query}`,
    imageUrl: `${DBConfig.staticShareUrl}health_timeline.png`
  })
}

const buildDataCenterMessage = (title, user) => {
  const query = getShareQuery({ user })
  return ({
    title: i18n.share.data_center_title(),
    path: `${scenes.dataCenter}?${query}`,
  })
}

const buildDataCenterTimeline = user => {
  const query = getShareQuery({ user })
  return ({
    title: i18n.share.data_center_title(),
    query
  })
}

const buildInviteDetail = (myPainManagement, user) => {
  const query = user?.Status === USER_STATUS.Registered ? getShareQuery({ myPainManagement, user }) : ''
  const path = `${scenes.homePage}?shareType=${SHARE_ROUTE_TYPE.invite}&${query}`
  return ({
    title: '最近头痛好了很多，你也来试试',
    path,
    imageUrl: Images.share.share
  })
}

const buildInternationalConsul = (title, user) => {
  const query = getShareQuery({ user })
  return ({
    title: '【彼爱医疗】海外专家在线咨询',
    path: `${scenes.internationalConsultation}?${query}`,
    imageUrl: Images.share.icon_share_international
  })
}

const buildInternationalConsulTimeline = user => {
  const query = getShareQuery({ user })
  return ({
    title: '【彼爱医疗】国际远程会诊-海外专家在线咨询',
    query
  })
}

const buildCreateLog = user => {
  const query = getShareQuery({ user })
  return ({
    title: i18n.share.create_log_title(),
    path: `${scenes.createPainLog}?${query}`,
    imageUrl: Images.share.share_patient_log
  })
}

const buildCreateLogTimeline = user => {
  const query = getShareQuery({ user })
  return ({
    title: i18n.share.create_log_title(),
    query
  })
}
const onShareAppMessage = {
  doctorDetail: buildDoctorDetailMessage,
  decisionFlowIndex: buildDecisionFlowIndexMessage,
  decisionResult: buildDecisionResultMessage,
  expertPage: buildExpertMessage,
  feedDetail: buildFeedDetailMessage,
  videoDetail: buildVideoDetailMessage,
  tagDetail: buildTagDetailMessage,
  managePlan: buildManagePlanMessage,
  meditation: buildMeditationMessage,
  homePage: buildHomeMessage,
  dataCenter: buildDataCenterMessage,
  planReportDetail: buildPlanReportDetailMessage,
  inviteDetail: buildInviteDetail,
  internationalConsultation: buildInternationalConsul,
  createPainLog: buildCreateLog,
}

const onShareTimeline = {
  doctorDetail: buildDoctorDetailTimeline,
  decisionFlowIndex: buildDecisionFlowIndexTimeline,
  decisionResult: buildDecisionResultTimeline,
  expertPage: buildExpertTimeline,
  feedDetail: buildFeedDetailTimeline,
  videoDetail: buildVideoDetailTimeline,
  tagDetail: buildTagDetailTimeline,
  managePlan: buildManagePlanTimeline,
  meditation: buildMeditationTimeline,
  homePage: buildHomeTimeline,
  dataCenter: buildDataCenterTimeline,
  internationalConsultation: buildInternationalConsulTimeline,
  createPainLog: buildCreateLogTimeline,
}

const onAddToFavorites = {
  doctorDetail: buildDoctorDetailTimeline,
  expertPage: buildExpertTimeline,
  feedDetail: buildFeedDetailTimeline,
  videoDetail: buildVideoDetailTimeline,
  homePage: buildHomeTimeline,
}

const shareManage = {
  onShareAppMessage,
  onShareTimeline,
  onAddToFavorites,
}

export default shareManage
